package com.yfuxdeer.youchat.adapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.pojo.FriendItem;
import com.yfuxdeer.youchat.pojo.User;
import com.yfuxdeer.youchat.service.FriendService;
import com.yfuxdeer.youchat.ui.RoundImageView;
import com.yfuxdeer.youchat.utils.ImageUtil;
import com.yfuxdeer.youchat.utils.Store;

import java.util.List;

/**
 * 主页显示消息列表的数据适配器
 */
public class SearchFriendListAdapter extends BaseAdapter {

    private final static String TAG = SearchFriendListAdapter.class.getSimpleName();
    private List<User> friendItemList;
    private Context context;
    private Store store = Store.getInstance();

    public SearchFriendListAdapter(List<User> friendItemList, Context context) {
        this.friendItemList = friendItemList;
        this.context = context;
        //注册广播
        context.registerReceiver(addFriendReceiver, new IntentFilter("com.yfuxdeer.youchat.addFriendReceiver"));
    }

    @Override
    public int getCount() {
        if (friendItemList == null) return 0;
        return friendItemList.size();
    }

    @Override
    public Object getItem(int i) {
        return friendItemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = View.inflate(context, R.layout.search_friend_item, null);
            holder = new ViewHolder();
            holder.head = view.findViewById(R.id.head);
            holder.nickName = view.findViewById(R.id.nickName);
            holder.figure = view.findViewById(R.id.figure);
            holder.add = view.findViewById(R.id.add);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        User user = friendItemList.get(i);

        ImageUtil.glideLoad(context, user.getUsername(), 0, holder.head);

        holder.nickName.setText(user.getNickName());
        holder.figure.setText(user.getFigure());
        ViewHolder finalHolder = holder;
        //子线程里访问网络
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    FriendItem friendItem = FriendService.findFriendById(store.getCurrentUser().getUsername(), user.getUsername());
                    if (friendItem == null) {
                        //添加好友
                        finalHolder.add.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Button btn = (Button) view;
                                //启动添加好友服务
                                Intent intent = new Intent(context, FriendService.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("method", FriendService.METHOD.ADD_FRIEND);
                                bundle.putString("host_id", store.getCurrentUser().getUsername());
                                bundle.putString("friend_id", user.getUsername());
                                intent.putExtras(bundle);
                                context.startService(intent);
                                //禁用按钮
                                btn.setClickable(false);
                                btn.setBackgroundColor(Color.argb(1.0f, 0.5f, 0.5f, 0.5f));
                                btn.setText("已添加");
                            }
                        });
                    } else {
                        finalHolder.add.setClickable(false);
                        finalHolder.add.setBackgroundColor(Color.argb(1.0f, 0.5f, 0.5f, 0.5f));
                        finalHolder.add.setText("已添加");
                    }
                } catch (Exception e) {
                    Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        }).start();
        return view;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public static class ViewHolder {
        public RoundImageView head;
        public TextView nickName;
        public TextView figure;
        public Button add;
    }

    private BroadcastReceiver addFriendReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            FriendService.RESULT result = (FriendService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}